<?php
class Model{
//	保存mysql连接对象
	private static $link=NULL;
//	保存表明
	private $table;
//	保存表的结构信息
	private $opt;
	/*
	 * 构造方法,实例化后自动运行
	 */
	 public function __construct($table){
	 	if(is_null(self::$link)){   //保存静态变量  加载为空时连接  之后不执行 
	 		$link = @new Mysqli(C('MYsql_path'),C('MYsql_user'),C('MYsql_password'),C('MYsql_datePath'));  //连接数据库
			if($link->connect_errno)die($link->connect_error);//如果连接错误提示错误信息
			$link->query("SET NAMES UTF8");//设置字符集		
			self::$link = $link;//把对象保存到属性中					
			$this->error(); //如果错误 就提示错误信息
	 	}
		$this->table = $table; //把表明保存到属性中
		$this->opt();//初始化表的结构
	 }
	 /*
	  * error 错误处理
	  */
	 private function error(){
	 	if(self::$link->errno) die(self::$link->error);       //判断是否有错误
	 }
	 /*
	  * 查询数据信息  组合查询语句结构
	  */
	 public function all(){
		$sql = "SELECT " . $this->opt['field'] . " FROM " . $this->table . $this->opt['where'] . $this->opt['group'] . $this-> opt['having'] . $this->opt['order'] . $this->opt['limit'];//调用opt组成链式操作
//		echo $sql;
		return $this->query($sql);
	 }
	 /*
	  * 定义查询语句结构
	  * */
	 private function opt(){
//select * from user where 1=1 group by uid having uid=1 order by  uid desc limit 1;
	 	$this->opt =array(
			'where' =>'',
			'field' =>'*',
			'order' =>'',
			'limit' =>'',
			'group' =>'',
			'having'=>''
		);
	 }
	 /*
	  * opt 查询语句架构调用项
	  */
	  //条件
	  public function where($where){
	  		$this->opt['where'] = " WHERE  {$where}";
	  		return $this;
	  }
	  //字段
	  public function field($field){
	  		$this->opt['field'] = $field;
			return $this;
	  }
	  //排序
	  public function order($order){
	  		$this->opt['order'] = " ORDER BY {$order} ";
	  		return $this;
	  }
	  //截取
	  public function limit($limit){
	  		$this->opt['limit'] = "LIMIT {$limit}";
			return $this;
	  }
	  //分组
	  public function group($group){
	  		$this->opt['group'] = "GROUP BY {$group}";
			return $this;
	  }
	  //包括
	  public function having($having){
	  		$this->opt['having'] = "HVAGING {$having}";
			return $this;
	  }
	 /*
	  * 执行有结果集的操作 做查询  不是mysqli的query 将组合好的路径信息传入执行
	  */
	 public function query($sql){
	 	$result = self::$link->query($sql); //传入值运行语句查询 并保存值赋给$resutl;
		$this->error();//判断  错误提示
		$rows = array();//定义数组
		while($row = $result->fetch_assoc()){  //将值存入数组
			$rows[] = $row;
		}
		$result->free(); //释放结果集
		return $rows; //返回结果
	 }
	 /*
	  * 无结果集操作
	  */
	 public function exec($sql){
	 	self::$link->query($sql); //执行mysql语句
		$this->error(); //错误提示 判断
	 }
	 /*
	  * 获取一条信息
	  */
	 public function find(){
	 	$data = $this->all();
		return current($data);
	 }
	 /*
	  * 添加
	  */
	 public function add($data=NULL){
	 	if(is_null($data)) $data =$_POST;//单行if语句可以不假大括号  传值和post提交两种情况
	 	$fields = implode(',',array_keys($data));//用，风格数据到字符传
	 	$values = '"' .implode('","',array_values($data)) .'"'; //组合 
		$sql = "INSERT INTO " . $this->table . "({$fields}) VALUES ({$values})"; //将去除字符串键名 键值 组合成sql语句
		$this->exec($sql);//执行sql语句
	 }
	 /*
	  * 删除
	  */
	 public function delete(){
	 	if(!$this->opt['where']){ //确认where传值
	 		header('Content-type:text/html;charset=utf-8');
			die('Warning ！不传where值将删除所有数据, 请慎重操作 ,当前操作已被拦截');
	 	}
		$sql = "DELETE FROM " . $this->table . $this->opt['where']; //组合语句
		$this->exec($sql); //执行语句
	 }
	/*
	 * 修改
	 */	 
	public function update($data=NULL){
		if(!$this->opt['where']){ //判断是否赋值 where 不然所有都被修改
			header('Content-type:text/htm;charset=utf-8');
			die('修改信息须确认修改内容位置');
		}
		if(is_null($data))$data =$_POST;
		$info = '';
		foreach ($data as $field =>$value){
			$info .=$field . '="' . $value . '",'; //组合字符串   键名=“键值” 方式组合
		}
		$info = rtrim($info,','); //去除最右边，  不然出错  UPDATE user SET username="asdfasd",password="asdfasd",uid="2", WHERE uid=2 where 前边不能多处,报错
		$sql = "UPDATE " . $this->table . " SET {$info} " . $this->opt["where"];//组合执行语句
		$this->exec($sql); //执行语句
 	}
	 
	 
	 
	 
	 
	 
	 
}

